package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.param.AdminUpdateStatusParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminRepositoryImpl implements AdminRepository {

    private static final Logger log = LoggerFactory.getLogger(AdminRepositoryImpl.class);
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public AdminPO getAdminByPhone(String phone) {
        QueryWrapper<AdminPO> query = new QueryWrapper<>();
        query.eq("account_phone",phone);
        return adminMapper.selectOne(query);
    }

    @Override
    public AdminPO getAdminById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminPO> pagesList(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = assembleAdminQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<AdminPO> pos = adminMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(pos);
    }


    @Override
    public long countUserById(Integer id) {
        return selectById(id);
    }

    @Override
    public void reset(Integer id) {
        log.info("持久化层-重置密码");
        //将密码重置为12345677
        AdminPO adminPO = getAdminById(id);
        adminPO.setAccountPassword("12345677");
        UpdateWrapper<AdminPO> updateWrapper = new UpdateWrapper<>() ;
        updateWrapper.set("account_password",adminPO.getAccountPassword());

        adminMapper.update(null,updateWrapper.eq("id",id));

    }

    @Override
    public long countUserByPhone(QueryWrapper<AdminPO> queryWrapper) {
        //1.accountPhone 账号手机号 相等查询 where account_phone=?
        return adminMapper.selectCount(queryWrapper);
    }

    @Override
    public void save(AdminPO poParam) {
        log.info("持久层-保存用户");
        adminMapper.insert(poParam);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    @Override
    public void update(AdminPO poParam) {
        log.info("持久层-更新用户");
        adminMapper.updateById(poParam);
    }

    @Override
    public void updateStatusById(AdminUpdateStatusParam query) {
        adminMapper.update(null,new UpdateWrapper<AdminPO>().eq("id",query.getId()).set("account_status",query.getAccountStatus()));
    }

    private long selectById(Integer id) {
        return adminMapper.selectCount(new QueryWrapper<AdminPO>().eq("id",id));
    }

    private QueryWrapper<AdminPO> assembleAdminQuery(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        //1.accountPhone 账号手机号 相等查询 where account_phone=#{phone}
        if (StringUtils.isNotBlank(query.getAccountPhone())){
            queryWrapper.eq("account_phone",query.getAccountPhone());
        }
        //2.nickname 昵称 模糊查询 非空 where nickname like(%#{}%)
        if (StringUtils.isNotBlank(query.getNickname())){
            queryWrapper.like("nickname",query.getNickname());
        }
        return queryWrapper;
    }
}
